﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShopB2C.Entity;
using System.Web.Security;
using System.Web.Script.Serialization;
using xTeam.HengLe.Entity;

namespace xTeam.HengLe.Controller.Website {
    public partial class BaseController : Controller.BaseController {

        protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
            base.Initialize(requestContext);
            // 从cookie中得到用户对象
            try {
                FormsAuthenticationTicket ticket = ((FormsIdentity)User.Identity).Ticket;
                JavaScriptSerializer json = new JavaScriptSerializer();
                _user = json.Deserialize<UserEntity>(ticket.UserData) ?? new UserEntity() { NickName = _anonymous, LevelName = _defaultRole };
            }
            catch { _user = new UserEntity() { NickName = _anonymous, LevelName = _defaultRole }; }
            ViewData["User"] = _user;

            int pageCount = 0;
            ViewData["Sales"] = _productDataAccess.Search(int.MaxValue, 1, 0, string.Empty, _user.Uid, null, ref pageCount)
                .OrderByDescending(m => m.SalesCount).Take(6).ToList() ?? new List<EProduct>();
            // ViewData["Sales"] = new List<EProduct>();

            ViewData["ImageRoot"] = _imageRoot;
        }
    }
}
